import axios, { type AxiosResponse, type AxiosRequestHeaders } from 'axios';

import { useUserInfoStore } from '../stores/module/user';

const request = axios.create({
    // baseURL: 'http://gmall-h5-api.atguigu.cn',
    baseURL: '/api',
    timeout: 20000
})

request.interceptors.request.use((config) => {
    const userInfoStore = useUserInfoStore()
    if (userInfoStore.token) {
        (config.headers as AxiosRequestHeaders).token = userInfoStore.token
    }
    return config;
}, err => {

    return Promise.reject(err)
})

request.interceptors.response.use((config) => {

    return config.data
}, err => {

    return Promise.reject(err)
})

export default request